package com.ruoyi.project.web.videoInfo.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.web.videoInfo.domain.VideoInfo;
import com.ruoyi.project.web.videoInfo.service.IVideoInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学习视频Controller
 * 
 * @author ruoyi
 * @date 2021-01-21
 */
@Controller
@RequestMapping("/web/videoInfo")
public class VideoInfoController extends BaseController
{
    private String prefix = "web/videoInfo";

    @Autowired
    private IVideoInfoService videoInfoService;

    @RequiresPermissions("web:videoInfo:view")
    @GetMapping()
    public String videoInfo()
    {
        return prefix + "/videoInfo";
    }

    /**
     * 查询学习视频列表
     */
    @RequiresPermissions("web:videoInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VideoInfo videoInfo)
    {
        startPage();
        List<VideoInfo> list = videoInfoService.selectVideoInfoList(videoInfo);
        return getDataTable(list);
    }

    /**
     * 导出学习视频列表
     */
    @RequiresPermissions("web:videoInfo:export")
    @Log(title = "学习视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoInfo videoInfo)
    {
        List<VideoInfo> list = videoInfoService.selectVideoInfoList(videoInfo);
        ExcelUtil<VideoInfo> util = new ExcelUtil<VideoInfo>(VideoInfo.class);
        return util.exportExcel(list, "videoInfo");
    }

    /**
     * 新增学习视频
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学习视频
     */
    @RequiresPermissions("web:videoInfo:add")
    @Log(title = "学习视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile videoFile, VideoInfo videoInfo) throws IOException {
        if (videoFile != null) {
            // 上传文件路径
            String photoPath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String videoSaveName = FileUploadUtils.upload(photoPath, videoFile);

            videoInfo.setVideoUrl(videoSaveName);
        } else {
            return error("视频未上传");
        }
        return toAjax(videoInfoService.insertVideoInfo(videoInfo));
    }

    /**
     * 修改学习视频
     */
    @GetMapping("/edit/{videoId}")
    public String edit(@PathVariable("videoId") Long videoId, ModelMap mmap)
    {
        VideoInfo videoInfo = videoInfoService.selectVideoInfoById(videoId);
        mmap.put("videoInfo", videoInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存学习视频
     */
    @RequiresPermissions("web:videoInfo:edit")
    @Log(title = "学习视频", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile videoFile,VideoInfo videoInfo) throws IOException {
        if (videoFile != null) {
            // 上传文件路径
            String photoPath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String videoSaveName = FileUploadUtils.upload(photoPath, videoFile);

            videoInfo.setVideoUrl(videoSaveName);
        }
        return toAjax(videoInfoService.updateVideoInfo(videoInfo));
    }

    /**
     * 删除学习视频
     */
    @RequiresPermissions("web:videoInfo:remove")
    @Log(title = "学习视频", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(videoInfoService.deleteVideoInfoByIds(ids));
    }
}
